package com.lixinkeji.widget


import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.RelativeLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.lixinkeji.base.R
import com.lixinkeji.base.databinding.TabBarLayoutBinding
import com.lixinkeji.extension.dp2px
import com.lixinkeji.extension.onClick
import q.rorbin.badgeview.QBadgeView


class AppTabBar : RelativeLayout {
    private var tabBarLayoutBinding: TabBarLayoutBinding? = null

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        tabBarLayoutBinding = TabBarLayoutBinding.inflate(LayoutInflater.from(context), this, true)
        iniView()
    }

    private val fragmentList = ArrayList<Fragment>()
    fun add(fragment: Fragment): AppTabBar {
        fragmentList.add(fragment)
        return this
    }

    private lateinit var fragmentManager: FragmentManager
    fun commit(fragmentManager: FragmentManager, containerViewId: Int) {
        this.fragmentManager = fragmentManager
        val beginTransaction = fragmentManager.beginTransaction()
        fragmentList.forEach {
            beginTransaction.add(containerViewId, it).hide(it)
        }
        beginTransaction.show(fragmentList[0]).commit()
    }

    private var tabIndex = 0
    fun setTab(tabIndex: Int) {
        when (tabIndex) {
            0 -> {
                tabBarLayoutBinding?.tab1?.isChecked = true
                tabBarLayoutBinding?.tab2?.isChecked = false
                tabBarLayoutBinding?.tab3?.isChecked = false
                tabBarLayoutBinding?.tab4?.isChecked = false
                tabBarLayoutBinding?.tab5?.isChecked = false
            }

            1 -> {
                tabBarLayoutBinding?.tab1?.isChecked = false
                tabBarLayoutBinding?.tab2?.isChecked = true
                tabBarLayoutBinding?.tab3?.isChecked = false
                tabBarLayoutBinding?.tab4?.isChecked = false
                tabBarLayoutBinding?.tab5?.isChecked = false
            }

            2 -> {
                tabBarLayoutBinding?.tab1?.isChecked = false
                tabBarLayoutBinding?.tab2?.isChecked = false
                tabBarLayoutBinding?.tab3?.isChecked = true
                tabBarLayoutBinding?.tab4?.isChecked = false
                tabBarLayoutBinding?.tab5?.isChecked = false
            }

            3 -> {
                tabBarLayoutBinding?.tab1?.isChecked = false
                tabBarLayoutBinding?.tab2?.isChecked = false
                tabBarLayoutBinding?.tab3?.isChecked = false
                tabBarLayoutBinding?.tab4?.isChecked = true
                tabBarLayoutBinding?.tab5?.isChecked = false
            }

            4 -> {
                tabBarLayoutBinding?.tab1?.isChecked = false
                tabBarLayoutBinding?.tab2?.isChecked = false
                tabBarLayoutBinding?.tab3?.isChecked = false
                tabBarLayoutBinding?.tab4?.isChecked = false
                tabBarLayoutBinding?.tab5?.isChecked = true
            }

        }
    }


    fun show(tabIndex: Int) {
        val last = System.currentTimeMillis()
        this.tabIndex = tabIndex
        val beginTransaction = fragmentManager.beginTransaction()
        fragmentList.forEach {
            if (it.isAdded) {
                beginTransaction.hide(it)
            }
        }
        beginTransaction.show(fragmentList[tabIndex]).commit()
        if (::onTabClickListener.isInitialized) {
            onTabClickListener(fragmentList[tabIndex], tabIndex)
        }

    }




    private lateinit var onReleaseClickListener: () -> Unit
    private lateinit var onTabClickListener: (Fragment, Int) -> Unit

    @JvmName("setOnReleaseClickListener1")
    fun setOnReleaseClickListener(onReleaseClickListener: () -> Unit) {
        this.onReleaseClickListener = onReleaseClickListener
    }

    fun setOnTabClickListener(onTabClickListener: (Fragment, Int) -> Unit) {
        this.onTabClickListener = onTabClickListener
    }

    private fun iniView() {
//        var drawable = resources.getDrawable(R.drawable.tab_1_yes)
//        drawable.setBounds(0, 0, context?.dp2px(27f)!!.toInt(), context?.dp2px(27f)!!.toInt())
//        tabBarLayoutBinding?.apply {
//            tab1.setCompoundDrawables(null,drawable,null,null)
//        }

        tabBarLayoutBinding?.tab1?.onClick {
            show(0)
        }
        tabBarLayoutBinding?.tab2?.onClick {
            show(1)
        }

        tabBarLayoutBinding?.tab3?.onClick {
            show(2)
        }
        tabBarLayoutBinding?.tab4?.onClick {
            show(3)
        }
        tabBarLayoutBinding?.tab5?.onClick {
            show(4)
        }
    }


//    fun setSquareCount(count: Int) {
//        QBadgeView(context)
//            .bindTarget(tabBarLayoutBinding?.tabSquareCount)
//            .setBadgeNumber(count)
//            .setShowShadow(false)
//            .setBadgeTextSize(10f, true)
//    }
//
//    fun setMineCount(count: Int) {
//        QBadgeView(context)
//            .bindTarget(tabBarLayoutBinding?.tabMineCount)
//            .setBadgeNumber(count).setShowShadow(false)
//            .setBadgeTextSize(10f, true)
//    }
}